package com.ghs.jms;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;


public class MessageSender {
	
	public static void Sender(String[] args) throws Exception {
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageProducer producer = null;
		try {
			connection = ConnUtils.createConnection(SystemSettings.BROKER_URL);
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue(SystemSettings.QUEUE);
			producer = session.createProducer(destination);
			sendMessage(session,producer,args);
		} catch (JMSException e) {
			e.printStackTrace();
		}finally{
			ConnUtils.free(session, connection);
		}
	}
	
	public static void sendMessage(Session session,MessageProducer producer,String[] args) throws Exception{
		for(int i=1; i<5; i++){
			String text = "登录"+i+"封邮件";

			try {
				TextMessage message = session.createTextMessage(text);
				System.out.println("发送邮件："+text);
				producer.send(message);	
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
}
